from jinja2 import Template
import yaml

from untils.log_ import logger


def get_keys(value):
    """
    获取字典中中所有key的递归函数
    :param value:
    :return:
    """
    if isinstance(value, dict):
        for key, nested_value in value.items():
            yield key
            yield from get_keys(nested_value)


def is_same(template: list, src: list):
    """
    判断两个列表是否相同
    :param template:
    :param src:
    :return:
    """
    index = len(template)
    for i in range(index):
        if template[i] not in src:
            logger.error(f"参数\t{template[i]}\t错误")
            return False
    logger.info("参数格式正确")
    return True


def validate_input(input_data):
    """
    验证输入参数格式
    :param input_data:
    :return:
    """
    # 加载YAML模板
    with open("ihrm_data.yaml", "r") as f:
        yaml_template = f.read()

    yaml_data = yaml.safe_load(yaml_template)

    key1 = list(get_keys(yaml_data))
    key2 = list(get_keys(input_data))

    if is_same(key1, key2):
        test_data = Template(yaml_template).render(input_data)
        return yaml.safe_load(test_data)
    else:

        return False





if __name__ == '__main__':
    input_data = {"method": "post", "url": "/api/sys/user", "json": {"mobile": "15565161664"}}

    print(type(validate_input(input_data)))
